﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace CPPEI.Net
{
    public class NetClientProxy
    {
        /// <summary>
        /// 通过此socket和接收客户端的数据，发送数据到客户端
        /// </summary>
        public Socket Socket { get; private set; }
        public UInt32 IP { get; set; }
        public uint Port { get; set; }
        public uint Id { get; set; }
        public uint RoomID { get; set; }

        public byte[] Buffer { get; set; }
        public PackagePool PackagePool { get; set; }


        public bool IsReceiving { get; set; }

        /// <summary>
        /// 是否已经认证通过
        /// </summary>
        public bool IsAuth { get; set; }

        /// <summary>
        /// 终端发生错误的次数
        /// </summary>
        public int ErrorCount { get; set; }

        public NetClientProxy(Socket socket)
        {
            Socket = socket;
            IP = IP2Int(((IPEndPoint)Socket.RemoteEndPoint).Address.ToString());
            Port = (uint)((IPEndPoint)Socket.RemoteEndPoint).Port;
            IsReceiving = false;
            Buffer = new byte[10*1024*1024];
            PackagePool = new PackagePool();
        }

        public void Send(Package package)
        {
            try
            {
                int len = Socket.Send(package.GetFullBuffer());
                System.Diagnostics.Debug.WriteLine(string.Format("发送的数据:{0}", len));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("发送数据出错");
            }
        }

        /// <summary>
        /// 发送数据到客户端
        /// </summary>
        /// <param name="buffer"></param>
        public void SendData(byte[] buffer)
        {
            int len = Socket.Send(buffer);
        }

        /// <summary>
        /// 发送数据到客户端
        /// </summary>
        /// <param name="buffer"></param>
        public void SendData(byte[] buffer, int length)
        {
            Socket.Send(buffer, length, SocketFlags.None);
        }


        /// <summary>
        /// 获取网络地址 ip:port
        /// </summary>
        /// <returns></returns>
        public string GetNetAddress()
        {
            return string.Format("{0}:{1}", ((IPEndPoint)Socket.RemoteEndPoint).Address, ((IPEndPoint)Socket.RemoteEndPoint).Port);
        }
        public static string Int2IP(UInt32 ipCode)
        {
            byte a = (byte)((ipCode & 0xFF000000) >> 0x18);
            byte b = (byte)((ipCode & 0x00FF0000) >> 0xF);
            byte c = (byte)((ipCode & 0x0000FF00) >> 0x8);
            byte d = (byte)(ipCode & 0x000000FF);
            string ipStr = String.Format("{0}.{1}.{2}.{3}", a, b, c, d);
            return ipStr;
        }
        public static UInt32 IP2Int(string ipStr)
        {
            string[] ip = ipStr.Split('.');
            uint ipCode = 0xFFFFFF00 | byte.Parse(ip[3]);
            ipCode = ipCode & 0xFFFF00FF | (uint.Parse(ip[2]) << 0x8);
            ipCode = ipCode & 0xFF00FFFF | (uint.Parse(ip[1]) << 0xF);
            ipCode = ipCode & 0x00FFFFFF | (uint.Parse(ip[0]) << 0x18);
            return ipCode;
        }
    }
}
